package com.example.cardgame

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
const val MAXSCORE_MSG="MAXSCORE_MSG"
class MainActivity : AppCompatActivity() {
    private val REQUEST_CODE_MASSAGE = 1
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val button_modified = findViewById<Button>(R.id.modified_button)
        button_modified.setOnClickListener {
            var intent = Intent(this, MessageActivity::class.java)
            startActivityForResult(intent, REQUEST_CODE_MASSAGE)
        }

        val button_game = findViewById<Button>(R.id.play_button)
        button_game.setOnClickListener {
            var intent = Intent(this, GameActivity::class.java)
            intent.putExtra(MAXSCORE_MSG, findViewById<TextView>(R.id.textView6).text.toString())
            startActivityForResult(intent, 2)

        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_MASSAGE && resultCode == Activity.RESULT_OK) {
            val username = data?.getStringExtra("username")
            val stuNum = data?.getStringExtra("stuNum")
            val stuPne = data?.getStringExtra("stuPne")
            val stuEml = data?.getStringExtra("stuEml")
            val hobby1 = data?.getBooleanExtra("hobby1", false)
            val hobby2 = data?.getBooleanExtra("hobby2", false)
            val hobby3 = data?.getBooleanExtra("hobby3", false)
            val hobby4 = data?.getBooleanExtra("hobby4", false)
            val sex = data?.getStringExtra("sex")
            val photoUri = data?.getStringExtra("photoUri")

            // 假设显示结果的TextView的ID是textView
            val textView_name = findViewById<TextView>(R.id.textView)
            textView_name.text = username
            val textView_sex = findViewById<TextView>(R.id.textView2)
            textView_sex.text = sex
            val textView_stuNum = findViewById<TextView>(R.id.textView3)
            textView_stuNum.text = stuNum
            val textView_stuPne = findViewById<TextView>(R.id.textView4)
            textView_stuPne.text = stuPne
            val textView_stuEml = findViewById<TextView>(R.id.textView5)
            textView_stuEml.text = stuEml
            val textView_score = findViewById<TextView>(R.id.textView6)
            val textView_hobby = findViewById<TextView>(R.id.textView7)
            val hobbyText = {
                var text = ""
                if (hobby1 == true) text += "唱歌 "
                if (hobby2 == true) text += "跳舞 "
                if (hobby3 == true) text += "游泳 "
                if (hobby4 == true) text += "跑步 "
                text
            }
            textView_hobby.text = hobbyText()

            if(photoUri!=null){
                val imageView = findViewById<ImageView>(R.id.imageView)
                imageView.setImageURI(Uri.parse(photoUri))
            }
            if(requestCode==2){
                if(resultCode==3){
                    textView_score.text="游戏最高分："+data?.getStringExtra(SCORE)
                }
            }
        }
    }
}